﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WinFormExam
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            string sql = "select * from Articles";
            var dt = DbHelper.GetDataTable(sql);
            dataGridView1.DataSource = dt;


        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }
        /// <summary>
        /// 增加
        /// </summary>
        private void button1_Click(object sender, EventArgs e)
        {
            Information addInfo = new Information();
            var cmd = addInfo.ShowDialog();
            if (cmd == DialogResult.Yes)
            {
                var Title = textBox1.Text;
                var sql = string.Format("select * from Articles where Title like '%{0}%'", Title);
                var dt = DbHelper.GetDataTable(sql);
                dataGridView1.DataSource = dt;
            }
        }
        /// <summary>
        /// 删除
        /// </summary>
        private void button3_Click(object sender, EventArgs e)
        {
            //if (dataGridView1.SelectedRows.Count > 0)
            //{
            //    var del = dataGridView1.SelectedRows[0];
            //    var id = (int)del.Cells["Id"].Value;
            //    var qus = MessageBox.Show("请确定删除", "删除警告", MessageBoxButtons.YesNo, MessageBoxIcon.Information);
            //    if (qus.Equals(DialogResult.Yes))
            //    {
            //        var sql = string.Format("delete from Articles where id = {0}", id);
            //        var coon = DbHelper.AddOrUpdateOrDelete(sql);
            //        if (coon > 0)
            //        {
            //            var Title = textBox1.Text;
            //            var cmd = string.Format("select * from Articles where Title like '%{0}%'", Title);
            //            var dt = DbHelper.GetDataTable(cmd);
            //            dataGridView1.DataSource = dt;

            //            MessageBox.Show("删除成功", "删除提示");
            //        }
            //    }
            //}
        }
            
        /// <summary>
        /// 查询
        /// </summary>
        private void button4_Click(object sender, EventArgs e)
        {
            string key = textBox1.Text;
            string Find = string.Format("select * from Articles where Title like '%{0}%' or Author like '%{0}%' or CreatedTime like '%{0}%'", key);
            dataGridView1.DataSource = DbHelper.GetDataTable(Find);
        }

        private void button2_Click(object sender, EventArgs e)
        {

        }
    }
}
